<template>
  <div class="edit-adjustment">
    <v-dialog title="新建应收调整" :show.sync="dialogVisible" :destroy-on-close="true" width="1200px" @beforeClose="beforeClose">
      <div class="card-box">
        <el-table :data="tableData" align="center" v-if="isRouterAlive">
          <el-table-column prop="propertyNames" label="物业名称" width="120"></el-table-column>
          <el-table-column prop="customerName" label="出租方" width="120"></el-table-column>
          <el-table-column prop="chargeProjectName" label="收款项目" width="120"></el-table-column>
          <el-table-column prop="costPeriodStart" label="费用周期" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.costPeriodStart}}至{{scope.row.costPeriodEnd}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="receivableDate" label="应收日期" width="120"></el-table-column>
          <el-table-column prop="receivableMoney" label="合同应收金额（元）" width="120"></el-table-column>
          <el-table-column prop="adjustmentAmount" label="本次调整金额（元）" width="120">
            <template slot-scope="scope">
              <el-input size="small" v-model="scope.row.adjustmentAmount" placeholder="请输入内容" @change="handleChangeAmount"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="adjustedAmount" label="调整后金额（元）" width="120"></el-table-column>
        </el-table>
      </div>
      <div class="card-box">
        <div class="card-title">备注</div>
        <div class="handling-opinions">
          <el-input v-model="remarks" placeholder="请输入" maxlength="100" class="full"></el-input>
        </div>
      </div>
      <div class="card-box">
        <div class="card-title">
            <div>
                <span class="required">*</span>上传文件
            </div>
        </div>
        <div class="handling-opinions">
          <v-upload
            ref="adjustmentUpload"
            :busId="projectId"
            modelType="finance"
            @onSuccess="onSuccess"
            isShowFileList
            :isView="false"
            multiple
            v-model="fileList"
          />
        </div>
      </div>
      <div class="card-box">
        <div class="card-title">
            <div>
                <span class="required">*</span>办理意见
            </div>
        </div>
        <div class="handling-opinions">
          <el-input type="textarea" v-model="opinion" maxlength="200" placeholder="请输入" class="full"></el-input>
        </div>
      </div>
      <span slot="foot">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button v-throttle="[commit]">保 存</el-button>
        <el-button v-throttle="[commitReview]">保存并提交复核</el-button>
      </span>
    </v-dialog>
  </div>
</template>

<script>
import { addAdjustment, getCollection} from '../../api/collection'

export default {
  name: "edit-adjustment",
  data () {
    return {
      dialogVisible: false,
      isRouterAlive: true,
      tableData: [],
      form: {},
      remarks: '',
      opinion: '',
      fileList: [],
      projectId: ''
    }
  },
  methods: {
    //打开弹框
    show (item) {
      this.dialogVisible = true
      this.projectId = item.adjustmentId
			getCollection(item.receivableId).then(res => {
        if (res.data.code === 0) {
          this.tableData.push(res.data.data)
					this.tableData[0].adjustmentAmount = ''
        }
      })
    },
    //关闭弹框
    beforeClose (done) {
      this.$emit('beforeClose')
    },
    //计算本次调整金额
    handleChangeAmount (val) {
      let reg = /^(([1-9]\d*)|(0))(\.\d{1,2})?$/;
      if (!reg.test(val)) {
        this.$message.error('数值格式错误（最多保留两位小数）')
        this.tableData[0].adjustmentAmount = ''
        this.tableData[0].adjustedAmount = ''
      } else {
        this.tableData[0].adjustedAmount = Number(this.tableData[0].receivableMoney) + Number(this.tableData[0].adjustmentAmount)
        this.isRouterAlive = false
        this.$nextTick(() =>
          this.isRouterAlive = true
        )
      }
    },
    //取消
    handleCancel () {
      this.dialogVisible = false
      this.tableData = []
      this.opinion = ''
			this.$emit('handleClose')
    },
    //保存
    commit () {
      if (this.tableData[0].adjustmentAmount === '') {
        this.err('请填写本次调整金额！')
      } else if (this.tableData[0].adjustmentAmount === 0) {
        this.err('调整金额不能为0！')
      } else if (this.opinion === '' || this.opinion === null || this.opinion === undefined) {
          this.$message.error('办理意见不能为空！')
      } else {
        this.form = this.tableData[0]
        this.form.adjustmentStatus = '10'
        this.$refs.adjustmentUpload.submit()
      }
    },
    //保存并提交复核
    commitReview () {
      if (this.tableData[0].adjustmentAmount === '') {
        this.err('请填写本次调整金额！')
      } else if (this.tableData[0].adjustmentAmount === 0) {
        this.err('调整金额不能为0！')
      } else if (this.opinion === '' || this.opinion === null || this.opinion === undefined) {
          this.$message.error('办理意见不能为空！')
      } else {
        this.form = this.tableData[0]
        this.form.adjustmentStatus = '11'
        this.$refs.adjustmentUpload.submit()
      }
    },
    //文件上传
    async onSuccess (response, file, fileList) {
      this.form.opinion = this.opinion
      this.form.remarks = this.remarks
			this.form.adjustmentId = response.data
      if (fileList === undefined || fileList.length <= 0) {
          this.$message.error('应收调整必须上传文件！')
      } else {
          const res = await addAdjustment(this.form)
          if (res.data.code == 0) {
              this.$message.success(`提交成功!`);
              this.dialogVisible = false;
              this.$emit('resetList')
          } else {
              this.$message.error(res.data.msg)
          }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.edit-adjustment {
  .card-box {
    margin-top: 20px;
    .handling-opinions {
      padding-top: 20px;
      .el-textarea__inner {
        border: 0.5px solid #ccc;
      }
    }
      .card-title {
          .required {
              color: #f67979;
              margin-right: 8px;
          }
      }
  }
}
</style>
